package epam.concurrency.lab2.task5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Standard bank account
 * @author Iurii Miedviediev
 * @version 1.0 Build 17.08.2014
 */
public class Account {

    private Long amount;
    private Lock lock;

    public Account(long amount) {
        this.amount = amount;
        this.lock = new ReentrantLock();
    }

    public long getAmount() {
        lock.lock();
        try {
            return this.amount;
        } finally {
            lock.unlock();
        }
    }

    /**
     * Check out a certain amount of money from current account
     * @param amt amount of $
     * @return true if there is enough money for the transaction, false otherwise
     */
    public boolean withdraw(long amt) {
        lock.lock();
        try {
            if(this.amount < amt) {
                return false;
            }
            this.amount -= amt;
            return true;
        } finally {
            lock.unlock();
        }
    }

    /**
     * Check in a certain amount of money to a certain account
     * @param amt amount of $
     */
    public void deposit(long amt) {
        lock.lock();
        try {
            this.amount += amt;
        } finally {
            lock.unlock();
        }

    }
}